/**
 * related to main.ui
 * 
 * @Author : wmn1988@qq.com
 * @Timestamp : 2016-10-17
 */

//引入组件库
var nf = sm("do_Notification");
var root = ui("$");
var do_App = sm("do_App");
var do_Page = sm("do_Page");
var hashdata = mm("do_HashData");
var mallUrl = require("url/mall");
var qqHttp = require("helper/http");
var httpSilence = require("helper/httpSilence");
var helperOpen = require("helper/open");

var nolistdata = mm("do_ListData");
var parentCategory = mm("do_ListData");
var childrenCategory = mm("do_ListData");

var listViewCategory=ui("do_ListView_1");
listViewCategory.bindItems(parentCategory);

var gridViewCategory = ui("do_GridView_1");
gridViewCategory.bindItems(childrenCategory);

// andriod返回键
do_Page.on("back", function() {
	do_App.closePage();
});

// 左上角-返回
ui("headerLeft").on("touch", function() {
	do_App.closePage()
});

ui("headerMiddle").on("touch",function(){
	helperOpen.open("source://view/mall/search/main.ui");
});

//初始化
do_Page.on("loaded", function() {
	loadPage();
});

function loadPage() {
	qqHttp.post(mallUrl.url.category.firstLevel, {}, function(data) {
		parentCategory.addData(data.categoryList);
		var defaultCell = parentCategory.getOne(0);
		parentCategory.updateOne(0,{
				"categoryId": defaultCell.categoryId,
				"name":defaultCell.name,
			    "FONTCOL":"FF8040FF",
				"BGCOL":"FFFFFFFF",
				"index":0
		});
		listViewCategory.refreshItems();
		
		childrenCategory.addData(data.defaultChildrenList);
		gridViewCategory.refreshItems();
	});
}

listViewCategory.on("touch",function(index){
	//page.fire("colorupdate",index);
	var cell = parentCategory.getOne(index);
	var parentId = cell.categoryId;
	for(var i=0;i<parentCategory.getCount();i++){
		var cell = parentCategory.getOne(i);
		parentCategory.updateOne(i, {
			"categoryId": cell.categoryId,
			"name":cell.name,
			"FONTCOL":i==index? "FF8040FF":"444444FF",
			"BGCOL":i==index? "FFFFFFFF":"F2F2F2FF",
			"index":i
		});
	}
	listViewCategory.refreshItems();
	//展示二级目录
	childrenCategory.removeAll();
	httpSilence.post(mallUrl.url.category.childrenList, {parentId : parentId}, function(data) {
		childrenCategory.addData(data.childrenList);
		gridViewCategory.refreshItems();
	});
	gridViewCategory.refreshItems();
});
